package se19team1e.gui.awt;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.List;
import java.util.Vector;
import javax.swing.BoxLayout;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import se19team1e.entity.JobAdvertDTO;
import se19team1e.gui.MessageBoxHelper;
import se19team1e.util.ReflectionUtils;
import se19team1e.util.StringUtil;

/**
 * 
 * A GUI panel for showing the job advert search fields. It will display a text
 * box for input and a table for output
 * 
 * @author Arun Manivannan
 * 
 */
public class GenericSearchPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	/**
	 * An awt button to submit login credentials.
	 */
	private JButton submitButton;

	private JTable table = null;
	private JTextField txtSearchInput;
	private JButton viewButton;
	private JButton editButton;
	private JButton deleteButton;
    private JButton withdrawButton;
    private JButton viewJobResponseButton;

	/**
	 * Constructor to create the generic search panel
	 * 
	 * @param columnHeader
	 *            Vector of Strings
	 * @param columnData
	 *            Vector of vector
	 */
	public GenericSearchPanel(Vector<String> columnHeader, Vector<Vector> columnData) {
    
   /* Vector<String> columnHeader=null; 
    Vector<Vector> columnData=null;
    String searchByLabel="Hello";
    
    public GenericSearchPanel() {*/

    	//private Vector<Customer> columnHeader;
    	BoxLayout thisLayout = new BoxLayout(this, javax.swing.BoxLayout.Y_AXIS);
		this.setLayout(thisLayout);
    	
		JPanel searchPanel=new JPanel();
		searchPanel.setLayout(new FlowLayout());
		
		
		txtSearchInput = new JTextField();
		txtSearchInput.setToolTipText("Clear text box for full search");
		searchPanel.add(txtSearchInput);
		txtSearchInput.setColumns(50);
		
		
		submitButton = new JButton("Search");
		
		searchPanel.add(submitButton);
		
		this.add(searchPanel);
		
		JPanel resultPanel=new JPanel();
		resultPanel.setLayout(new BorderLayout());
		//Testing
		columnHeader = ReflectionUtils.getFieldsInUpperCase(new JobAdvertDTO());
    	columnData= ReflectionUtils.getEmptyColumnData(columnHeader);
    	
		GenericJTableModel genericTableModel = new GenericJTableModel(columnHeader, columnData);
		table = new JTable(genericTableModel);
		
		
		DefaultTableModel headerData = new DefaultTableModel();
		headerData.addRow(columnHeader.toArray()); 
        JTable rowHeader = new JTable(headerData);

        rowHeader.setIntercellSpacing(new Dimension(0, 0));
        Dimension d = rowHeader.getPreferredScrollableViewportSize();
        d.width = rowHeader.getPreferredSize().width;
        rowHeader.setPreferredScrollableViewportSize(d);
        rowHeader.setRowHeight(table.getRowHeight());
        
        JScrollPane scrollPane = new JScrollPane(table);
        
        scrollPane.setColumnHeaderView(rowHeader);

        JTableHeader corner = rowHeader.getTableHeader();
        corner.setReorderingAllowed(false);
        corner.setResizingAllowed(false);
        
        scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, corner);
        scrollPane.setVisible(true);

        //resultPanel.add(scrollPane);
        resultPanel.add(scrollPane, BorderLayout.CENTER);
        
        JPanel selectPanel=new JPanel();
        selectPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        viewButton = new JButton();
    	selectPanel.add(getViewButton());
    	viewButton.setText("View");
        editButton = new JButton();
    	selectPanel.add(getEditButton());
    	editButton.setText("Edit");
    	deleteButton = new JButton();
    	selectPanel.add(getDeleteButton());
    	deleteButton.setText("Delete");
    	deleteButton.setEnabled(false);
    	withdrawButton = new JButton();
        selectPanel.add(getWithdrawButton());
        withdrawButton.setText("Withdraw");
        viewJobResponseButton = new JButton();
        selectPanel.add(getViewJobResponseButton());
        viewJobResponseButton.setText("View Job Responses");
        
        
        this.add(resultPanel);
        
        this.add(selectPanel);
        
	}

	/**
	 * To inform the user if any mistakes are made
	 * 
	 * @param messageBoxHelper
	 *            Call the helper class to displays the message box
	 * @return return true if search input text is empty or null
	 */
	public boolean validateInput(MessageBoxHelper messageBoxHelper) {
		// check for empty entry
		if (StringUtil.isEmptyOrNull(this.txtSearchInput.getText())) {
			// failure, display error message
			messageBoxHelper.displayMissingInputErrorMessage("Please enter a search criteria. E.g. Oracle for company name ");

			return false;
		}
		return true;
	}
	

	/**
	 * To show submit button for the generic search panel
	 * 
	 * @return submit button object
	 */
	public JButton getSubmitButton() {
		return submitButton;
	}

	/**
	 * To set submit button for the generic search panel
	 * 
	 * @param submit
	 *            button object
	 * 
	 */
	public void setSubmitButton(JButton submitButton) {
		this.submitButton = submitButton;
	}

	/**
	 * To get the Jtable
	 * 
	 * @return table object
	 */
	public JTable getTable() {
		return table;
	}

	/**
	 * To set the Jtable table
	 * 
	 * @param table
	 *            object
	 */
	public void setTable(JTable table) {
		this.table = table;
	}

	/**
	 * To get the text search input field
	 * 
	 * @return JtextField txtSearchInput
	 */
	public JTextField getTxtSearchInput() {
		return txtSearchInput;
	}

	/**
	 * To set the text search input field
	 * 
	 * @param JtextField
	 *            txtSearchInput
	 */
	public void setTxtSearchInput(JTextField txtSearchInput) {
		this.txtSearchInput = txtSearchInput;
	}

	/**
	 * To convert the search results to a display format
	 * 
	 * @param searchResults
	 *            list of JobAdvert DTO
	 * @return columndata
	 */
	public Vector<Vector> convertSearchResultsToDisplayFormat(List<JobAdvertDTO> searchResults, Vector<String> columnHeader) {
		
		Vector<Vector> columnData=new Vector<Vector>();
		Vector<String> colDataTemp=null;
		
		if (searchResults!=null){
			for (JobAdvertDTO jobAdvertDTO : searchResults) {

				colDataTemp=new Vector<String>();
				
				//System.out.println("Column headers : "+columnHeader);
				
				for (String columnName: columnHeader) {
					
					colDataTemp.add(ReflectionUtils.getValueFromFieldName(jobAdvertDTO, columnName));
					
				}
				
				columnData.add(colDataTemp);
				
			}
		}
		
		return columnData;
		
	}
	
	public JButton getViewButton() {
		return viewButton;
	}
	
	public JButton getEditButton() {
		return editButton;
	}
	
	public JButton getDeleteButton() {
		return deleteButton;
	}
	
    public JButton getWithdrawButton() {
        return withdrawButton;
    }

	public JButton getViewJobResponseButton() {
		return viewJobResponseButton;
	}
}
